package edu.uc.ui;

import com.foodview1.R;
import edu.uc.dto.User;
import edu.uc.persistence.UserDAOInterface;
import edu.uc.persistence.UserDAO;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * Create User activity test stub. 
 * 
 * 
 * @author Robert
 * 
 * This class contains the code for the createNewUser screen
 *
 */
public class CreateUserActivity extends MainActivity {

	// Initialize variables
	private Button btnSubmit;
	private Button btnCancel;
	private EditText edtNewFirstName;
	private EditText edtNewLastName;
	private EditText edtNewEmailAddress;
	private EditText edtNewPassword;
	private EditText edtNewPasswordConfirm;
	private String firstName;
	private String lastName;
	private String email;
	private String password;
	private String passwordConfirm;

	//(Robert) Declare the create object
	UserDAOInterface create;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.create_user);
		setTitle("Food View");

		// Call Page Initializer
		init();
	}

	public void init() {
		
		// Create context object
		create = new UserDAO(this);

		// Define page variables
		btnSubmit = (Button)findViewById(R.id.btnSubmit);
		edtNewFirstName = (EditText)findViewById(R.id.tbFirstName);
		edtNewLastName = (EditText)findViewById(R.id.tbLastName);
		edtNewEmailAddress = (EditText)findViewById(R.id.tbEmail);
		edtNewPassword = (EditText)findViewById(R.id.tbPassword);
		edtNewPasswordConfirm = (EditText)findViewById(R.id.tbPasswordConfirm);

		// Create new Back button object
		btnCancel = (Button)findViewById(R.id.btnCancel);

		// Register the onClick listener
		btnCancel.setOnClickListener(new OnClickListener() {

			// OnClick Method
			// ~ Sends user back to main menu
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent clickCancel = new Intent(v.getContext(), loginActivity.class);
				startActivityForResult(clickCancel, 0);
				finish();
			}
		});	


		//Register the onClick listener
		btnSubmit.setOnClickListener(new OnClickListener(){

			// OnClick Method
			// ~ 
			public void onClick(View v){

				// Define variables from text input
				firstName = edtNewFirstName.getText().toString();
				lastName = edtNewLastName.getText().toString();
				email = edtNewEmailAddress.getText().toString();
				password = edtNewPassword.getText().toString();
				passwordConfirm = edtNewPasswordConfirm.getText().toString();

				//if password and confirmation don't match popup a message
				if (firstName.equals("") || lastName.equals("") || email.equals("") || password.equals("")) {

					Toast.makeText(context, "Must fill all fields!", Toast.LENGTH_LONG).show();

				} else {
					if(password.equals(passwordConfirm)){

						// Save the user to DB
						saveNewUser();

						// Log the new user in
						loginNewUser();

						// Inform the user
						Toast.makeText(context, "User created! Logging in...", Toast.LENGTH_LONG).show();

						// Continue to main screen
						Intent clickSubmit = new Intent(v.getContext(), MainActivity.class);
						startActivityForResult(clickSubmit, 1);
					}
					else {
						Toast.makeText(context, "Passwords do not match!", Toast.LENGTH_LONG).show();
					}
				}
			}
		});
	}

	protected void saveNewUser() {
		Log.i(getClass().getName(), "Saving new user");
		User NewUser = new User();

		// Define NewUser variables
		NewUser.setFirstName(edtNewFirstName.getText().toString());
		NewUser.setLastName(edtNewLastName.getText().toString());
		NewUser.setEmail(edtNewEmailAddress.getText().toString());
		NewUser.setPassword(edtNewPassword.getText().toString());

		try {
			// Save the new user
			create.save(NewUser);
		} catch (Exception e) {
			Log.e(getClass().getName(), "Error saving new user: " + e.getMessage());
			Toast.makeText(this, "Error saving new user: " + e.getMessage(), Toast.LENGTH_LONG).show();
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		Log.i(getClass().getName(), "Saved User: " + NewUser);
	}

	protected void loginNewUser() {
		Log.i(getClass().getName(), "Logging in...");
		User NewUser = new User();

		// Define NewUser variables
		NewUser.setFirstName(edtNewFirstName.getText().toString());
		NewUser.setLastName(edtNewLastName.getText().toString());
		NewUser.setEmail(edtNewEmailAddress.getText().toString());
		NewUser.setPassword(edtNewPassword.getText().toString());

		try {
			// Log the new user in
			create.logon(NewUser.getEmail(), NewUser.getPassword());
		} catch (Exception e) {
			Log.e(getClass().getName(), "Error logging in: " + e.getMessage());
			Toast.makeText(this, "Error logging in: " + e.getMessage(), Toast.LENGTH_LONG).show();
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		Log.i(getClass().getName(), "Logged in as: " + NewUser);
	}
}